﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using WebGodClient.ServiceReference1;

namespace WebGodClient
{
  public partial class FindHumansForm : Form
  {
    private readonly ServiceController serviceController;

    private readonly BindingSource bindingSource;

    public FindHumansForm()
    {
      InitializeComponent();
      serviceController = new ServiceController();
      childSxComboBox.DataSource = new[]
                              {
                                Sexes.All,
                                Sexes.Female, 
                                Sexes.Male
                              };
      childSxComboBox.SelectedItem = Sexes.All;
      bindingSource = new BindingSource();
      resultDataGridView.DataSource = bindingSource;
    }

    private void useAgeChecker_CheckedChanged(object sender, EventArgs e)
    {
      EnableChildAgeSubform(useChildAgeChecker.Checked);
    }

    private void useMarkChecker_CheckedChanged(object sender, EventArgs e)
    {
      EnableChildMarkSubform(useChildMarkChecker.Checked);
    }

    private void useParentAgeChecker_CheckedChanged(object sender, EventArgs e)
    {
      EnableParentAgeSubform(useParentAgeChecker.Checked);
    }

    private void parentNameTextBox_TextChanged(object sender, EventArgs e)
    {
      string parentName = parentNameTextBox.Text;
      UpdateChildSurnameText(parentName);
    }

    private void childMarkFromNumericUpDown_ValueChanged(object sender, EventArgs e)
    {
      UpdateParentMoneyText();
    }

    private void childMarkToNumericUpDown_ValueChanged(object sender, EventArgs e)
    {
      UpdateParentMoneyText();
    }

    private void childAgeFromNumericUpDown_ValueChanged(object sender, EventArgs e)
    {
      UpdateChildAgeText();
    }

    private void childAgeToNumericUpDown_ValueChanged(object sender, EventArgs e)
    {
      UpdateChildAgeText();
    }

    private void resetFormButton_Click(object sender, EventArgs e)
    {
      childMarkFromNumericUpDown.Value = childMarkFromNumericUpDown.Minimum;
      childMarkToNumericUpDown.Value = childMarkToNumericUpDown.Maximum;
      childAgeFromNumericUpDown.Value = childAgeFromNumericUpDown.Minimum;
      childAgeToNumericUpDown.Value = childAgeToNumericUpDown.Maximum;
      parentNameTextBox.Text = "";
      childNameTextBox.Text = "";
      useChildMarkChecker.Checked = false;
      useChildAgeChecker.Checked = false;
    }

    private void sendQueryButton_Click(object sender, EventArgs e)
    {
      var req = ConstructRequest();
      ResponseData[] response = serviceController.SendRequest(req);
      ProcessResponse(response);
    }

    private void ProcessResponse(ResponseData[] response)
    {
      bindingSource.DataSource = response;
      XmlResponseSaver.Save(response);
    }

    private SearchRequest ConstructRequest()
    {
      SearchRequest req = new SearchRequest();
      req.Name = childNameTextBox.Text.Trim();
      req.ParentName = parentNameTextBox.Text.Trim();
      req.Sex = (Sexes) childSxComboBox.SelectedItem;
      if (useParentAgeChecker.Checked)
      {
        req.ParentAge = new IntervalOfInt32()
                          {
                            From = Decimal.ToInt32(parentAgeFromNumericUpDown.Value),
                            To = Decimal.ToInt32(parentAgeToNumericUpDown.Value)
                          };
      }
      if (useChildAgeChecker.Checked)
      {
        req.ChildAge = new IntervalOfInt32()
                         {
                           From = Decimal.ToInt32(childAgeFromNumericUpDown.Value),
                           To = Decimal.ToInt32(childAgeToNumericUpDown.Value)
                         };
      }
      if (useChildMarkChecker.Checked)
      {
        req.AvgMark = new IntervalOfDouble()
                        {
                          From = Decimal.ToDouble(childMarkFromNumericUpDown.Value),
                          To = Decimal.ToDouble(childMarkToNumericUpDown.Value)
                        };
      }
      return req;
    }

    private void parentAgeFromNumericUpDown_ValueChanged(object sender, EventArgs e)
    {
      UpdateParentAgeText();
    }

    private void parentAgeToNumericUpDown_ValueChanged(object sender, EventArgs e)
    {
      UpdateParentAgeText();
    }


    private void EnableChildAgeSubform(bool enabled)
    {
      ageFromLabel.Enabled = enabled;
      ageToLabel.Enabled = enabled;
      childAgeFromNumericUpDown.Enabled = enabled;
      childAgeToNumericUpDown.Enabled = enabled;
    }
    private void EnableChildMarkSubform(bool enabled)
    {
      markFromLabel.Enabled = enabled;
      markToLabel.Enabled = enabled;
      childMarkFromNumericUpDown.Enabled = enabled;
      childMarkToNumericUpDown.Enabled = enabled;
      if (enabled)
      {
        UpdateParentMoneyText();
      }
      else
      {
        moneyLabel.Text = "";
      }
    }
    private void EnableParentAgeSubform(bool enabled)
    {
      parentAgeFromNumericUpDown.Enabled = enabled;
      parentAgeToNumericUpDown.Enabled = enabled;
      label6.Enabled = enabled;
      label7.Enabled = enabled;
    }

    private void UpdateChildSurnameText(string parentName)
    {
      string childSurnameText = string.Empty;
      if (parentName != string.Empty)
      {
        childSurnameText = string.Format("Отчества детей: *{0}*{1}*/*{0}*{2}*", parentName, "ович", "овна");
      }
      childSurnameLabel.Text = childSurnameText;
    }

    private void UpdateParentMoneyText()
    {
      decimal markFrom = childMarkFromNumericUpDown.Value;
      decimal markTo = childMarkToNumericUpDown.Value;
      string moneyText;
      Color moneyLabelColor;
      if(markFrom > markTo)
      {
        moneyText = string.Format("Введены неверные значения: {0:F} больше {1:F}", markFrom, markTo);
        moneyLabelColor = Color.Red;
      }
      else
      {
        double moneyFrom = Math.Pow(10, Decimal.ToDouble(markFrom));
        double moneyTo = Math.Pow(10, Decimal.ToDouble(markTo));

        moneyText = string.Format("Количество денег: От {0:F} до {1:F}", moneyFrom, moneyTo);
        moneyLabelColor = Color.Black;
      }
      moneyLabel.Text = moneyText;
      moneyLabel.ForeColor = moneyLabelColor;
    }

    private void UpdateChildAgeText()
    {
      decimal markFrom = childAgeFromNumericUpDown.Value;
      decimal markTo = childAgeToNumericUpDown.Value;

      if(markFrom > markTo)
      {
        string text = string.Format("Введены неверные значения: {0} больше {1}", markFrom, markTo);
        ageErrorMarkerLabel.Text = text;
        ageErrorMarkerLabel.ForeColor = Color.Red;
      }
      else
      {
        ageErrorMarkerLabel.Text = "";
      }

    }

    private void UpdateParentAgeText()
    {
      decimal markFrom = parentAgeFromNumericUpDown.Value;
      decimal markTo = parentAgeToNumericUpDown.Value;

      if (markFrom > markTo)
      {
        string text = string.Format("Введены неверные значения: {0} больше {1}", markFrom, markTo);
        parentAgeErrorMarker.Text = text;
        parentAgeErrorMarker.ForeColor = Color.Red;
      }
      else
      {
        parentAgeErrorMarker.Text = "";
      }

    }

    
  }
}
